package com.joeyzh.utils;

import android.content.Context;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

/**
 * Created by Joey
 * @author Joey
 * 焦点切换工具类，用于EditText焦点获取、取消操作
 */
public class FocusSwitchHelper {

    /**
     * 将焦点放在输入框中并选中全部 如果想要选中输入框中的文本必须要将焦点放在输入框中 如果想要焦点在输入框中必须设置下面三个方法
     *
     * @param editText
     */
    public static void requestScanFocus(EditText editText) {

        editText.setFocusable(true);
        editText.setFocusableInTouchMode(true);
        editText.requestFocus();
        editText.selectAll();
    }

    public static void requestScanFocusWithHideInput(Context context, EditText editText) {

        editText.setFocusable(true);
        editText.setFocusableInTouchMode(true);
        editText.requestFocus();
        editText.selectAll();
        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);

    }

    public static void clearFocusWithHideInput(View view) {
        hideInput(view);
        view.clearFocus();
    }


    public static void hideInput(View view) {
        InputMethodManager imm = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);

    }

}
